package junit.admin;

import com.jfinal.aop.Inject;
import io.jboot.test.MockMvcResult;
import io.jpress.model.User;
import io.jpress.service.UserService;
import junit.base.AdminBaseTester;
import org.junit.Assert;
import org.junit.Test;

import java.util.Map;

public class AdminUserTester extends AdminBaseTester {


    @Inject
    private UserService userService;


    @Test
    public void startRoleTestCases() {

        //用户登录，默认账号 admin 123456
        doLogin();

        //新增用户
        testAddUser();

        //更新用户
        testUpdateUser();

        //使用新的账户进行登录
        testNewUserLogin();

        //删除用户
        testDeleteUser();

    }


    /**
     * 新增用户
     */
    public void testAddUser() {
        User user = new User();
        user.setUsername("admin");

        Map<String, Object> paras = buildParas(user, "user");
        MockMvcResult mvcResult = postWithCsrfToken("/admin/user/doAdd", paras);
        Assert.assertTrue(mvcResult.getContent().contains("密码不能为空"));

        paras.put("newPwd", "123456");
        mvcResult = postWithCsrfToken("/admin/user/doAdd", paras);
        Assert.assertTrue(mvcResult.getContent().contains("确认密码不能为空"));


        paras.put("confirmPwd", "111111");
        mvcResult = postWithCsrfToken("/admin/user/doAdd", paras);
        Assert.assertTrue(mvcResult.getContent().contains("两次输入密码不一致"));


        paras.put("confirmPwd", "123456");
        mvcResult = postWithCsrfToken("/admin/user/doAdd", paras);
        Assert.assertTrue(mvcResult.getContent().contains("该用户名已经存在"));

        user.setUsername("admin888");
        paras.putAll(buildParas(user, "user"));
        mvcResult = postWithCsrfToken("/admin/user/doAdd", paras);

        Assert.assertTrue(mvcResult.getStatus() == 200);
        Assert.assertTrue(mvcResult.getContentAsJSONObject().get("state").equals("ok"));

        Long userId = mvcResult.getContentAsJSONObject().getLong("id");
        putContainerData("userId", userId);

        mvcResult.printResult();
    }


    /**
     * 修改用户信息
     */
    public void testUpdateUser() {

        User user = new User();
        user.setId((Long) getContainerData("userId"));
        user.setSignature("这真是一个美好的下午。");

        Map<String, Object> paras = buildParas(user, "user");

        MockMvcResult mvcResult = postWithCsrfToken("/admin/user/doSaveUser", paras);
        mvcResult.printResult();

        Assert.assertTrue(mvcResult.getStatus() == 200);
        Assert.assertTrue(mvcResult.getContentAsJSONObject().get("state").equals("ok"));

        Long userId = mvcResult.getContentAsJSONObject().getLong("id");
        putContainerData("userId", userId);

        final User byId = userService.findById(userId);
        Assert.assertEquals(byId.getSignature(), user.getSignature());
    }


    //使用新的账户进行登录
    public void testNewUserLogin() {
        Long userId = (Long) getContainerData("userId");
        User newUser = userService.findById(userId);

        final MockMvcResult mvcResult = doLogin(newUser.getUsername(), "123456");

        //未分配角色，没有角色的用户无法登录后台。
        Assert.assertTrue(mvcResult.getContent().contains("您没有登录的权限"));

        //退出
        doLogout();

        //使用超级账号登录
        doLogin();
    }

    /**
     * 删除用户
     */
    public void testDeleteUser() {
        Long userId = (Long) getContainerData("userId");
        MockMvcResult mvcResult = getWithCsrfToken("/admin/user/doUserDel/" + userId);
        mvcResult.printResult();

        Assert.assertTrue(mvcResult.getStatus() == 200);
    }


}
